<?php
  require_once $_SERVER['DOCUMENT_ROOT'] . '/proyecto/clases/autoload.inc.php';
  require_once ($_SERVER['DOCUMENT_ROOT'] .'/proyecto/modulos/informes/config/lang/eng.php');
  require_once ($_SERVER['DOCUMENT_ROOT'] . '/proyecto/modulos/informes/tcpdf.php');
  if (Zen::IniciarSesion())
  {
     $id = stripslashes(htmlspecialchars(trim($_GET['id'])));
     $rs = Zen::Consulta("SELECT 	OT.OT_ID AS ID_ATENCION, 
	OT_FECHA_ING AS FECHA_EXAMEN, 
	OT.PAC_RUT AS RUT_PACIENTE, 
	UPPER(PAC_NOMBRES || ' ' || PAC_APPATERNO || ' ' || PAC_APMATERNO) AS PAC_NOMBRE,
	(SELECT UPPER(CON_RUT) FROM EXAMENES_OT AS EXOT WHERE EXOT.OT_ID = OT.OT_ID LIMIT 1) AS RUT_CONVENIO,
	(SELECT UPPER(CON.CON_NOMBRE) FROM EXAMENES_OT AS EXOT JOIN CONVENIOS AS CON ON CON.CON_RUT = EXOT.CON_RUT WHERE EXOT.OT_ID = OT.OT_ID LIMIT 1) AS NOMBRE_CONVENIO,
	UPPER(OT.MET_RUT) AS RUT_MED_TRATANTE,
	UPPER(MET_NOMBRES || ' ' || MET_APPATERNO ||' '|| COALESCE(MET_APMATERNO, '')) AS NOMBRE_MED_TRATANTE,
	UPPER(OT.MEI_RUT) AS RUT_INFORMANTE,
	UPPER(MEI_NOMBRES || ' ' || MEI_APPATERNO ||' '|| COALESCE(MEI_APMATERNO, '')) AS NOMBRE_INFORMANTE
 
FROM	ORDENES_TRABAJO AS OT JOIN PACIENTES AS PAC ON OT.PAC_RUT = PAC.PAC_RUT
	JOIN MED_TRATANTES AS MET ON  MET.MET_RUT = OT.MET_RUT
	JOIN MEDICOS_INFORMANTES AS MEI ON MEI.MEI_RUT = OT.MEI_RUT

WHERE 	OT_ID = $id;");
/**
 * Creates an example PDF TEST document using TCPDF
 * @package com.tecnick.tcpdf
 * @abstract TCPDF - Example: Custom Header and Footer
 * @author Nicola Asuni
 * @copyright 2004-2009 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
 * @link http://tcpdf.org
 * @license http://www.gnu.org/copyleft/lesser.html LGPL
 * @since 2008-03-04
 */
class MYPDF extends TCPDF {
	public function Header() {
	    $fecha=date("d-m-Y H:i:s");
		$image_file = K_PATH_IMAGES.'logo.png';
		$this->Image($image_file, 10, 10, 15, '', 'PNG', '', 'T', false, 0, '', false, false, 0, false, false, false);
		$this->SetFont('times', 'B', 8);
        $this->SetXY(25,11);
        $this->Cell( 0, 0, 'Centro de Imagenología S.A.', 0, false, 'L', 0, '', 0, false, 'M', 'M');
        $this->SetXY(25,15);
        $this->Cell( 0, 0, 'Administración y Finanzas', 0, false, 'L', 0, '', 0, false, 'M', 'M');
        $this->SetXY(25,9.5);
        $this->Cell( 0, 0, $fecha, 0, false, 'R', 0, '', 0, false, 'M', 'M');
        $this->SetXY(25,14);
        $this->Cell( 0, 0, $_SESSION['usuario'], 0, false, 'R', 0, '', 0, false, 'M', 'M');
        $this->ln(4.5);
        $this->Cell(190, 0, 'Página '.$this->getAliasNumPage().' de '.$this->getAliasNbPages(), 0, false, 'R', 0, '', 0, false, 'T', 'M');
	}
	public function Footer() {
	   $this->SetX(170);
	   $this->Cell(30, 0, 'Fecha Probable de Entrega: 28-05-2010', 0, $ln=0, 'R', 7, '', 0, false, '', '');
	}
}
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 003');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->setLanguageArray($l);
$pdf->SetFont('times', 'B', 12);
$pdf->AddPage();
$txt = <<<EOD
Comprobante de Atención
EOD;
$pdf->SetFont('times', 'B', 14);
$pdf->Write($h=0, $txt, $link='', $fill=0, $align='C', $ln=true, $stretch=0, $firstline=false, $firstblock=false, $maxh=0);
$pdf->SetFont('times', 'U', 10);
$pdf->ln(10);
$pdf->SetX(15);
$pdf->SetFont('times', 'B', 10);
$pdf->Cell( 0, 0, 'Datos de la Atención', 0, false, 'L', 0, '', 0, false, 'M', 'M');
$pdf->SetFont('times', '', 10);
$pdf->ln(4.5);
$html = <<<EOF
<hr width="635" />
EOF;
$pdf->writeHTML($html, true, false, false, false, '');
foreach ($rs as $k => $record){ 
$a1=$record['id_atencion'];
$a2=$record['fecha_examen'];
$a3=$record['rut_paciente'];
$a4=$record['pac_nombre'];
$a5=$record['rut_convenio'];
$a6=$record['nombre_convenio'];
$a7=$record['rut_med_tratante'];
$a8=$record['nombre_med_tratante'];
$a9=$record['rut_informante'];
$a10=$record['nombre_informante'];
$tbl = <<<EOD
<table border="0" cellpadding="2" cellspacing="2">
<tr><td></td></tr>
<tr>
<td width="150">&nbsp;&nbsp;&nbsp;<b>ID Atención:</b></td><td width="100">&nbsp;&nbsp;&nbsp;$a1</td><td width="400">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Fecha Emision:</b>$a2</td>
</tr>
<tr>
<td>&nbsp;&nbsp;&nbsp;<b>Paciente:</b></td><td>&nbsp;&nbsp;&nbsp;$a3</td><td>&nbsp;&nbsp;&nbsp;$a4</td>
</tr>
<tr>
<td>&nbsp;&nbsp;&nbsp;<b>Convenio:</b></td><td>&nbsp;&nbsp;&nbsp;$a5</td><td>&nbsp;&nbsp;&nbsp;$a6</td>
</tr>
<tr>
<td>&nbsp;&nbsp;&nbsp;<b>Médico Tratante:</b></td><td>&nbsp;&nbsp;&nbsp;$a7</td><td>&nbsp;&nbsp;&nbsp;$a8</td>
</tr>
<tr>
<td>&nbsp;&nbsp;&nbsp;<b>Radiólogo Informante:</b></td><td>&nbsp;&nbsp;&nbsp;$a9</td><td>&nbsp;&nbsp;&nbsp;$a10</td>
</tr>
</table>
EOD;
$pdf->writeHTML($tbl, true, false, false, false, '');
break;
}
$pdf->SetXY(15,95);
$pdf->SetFont('times', 'B', 10);
$pdf->Cell( 0, 0, 'Datos de los Exámenes', 0, false, 'L', 0, '', 0, false, 'M', 'M');
$pdf->SetFont('times', '', 10);
$pdf->ln(4.5);
$html = <<<EOF
<hr width="635" />
EOF;
$pdf->writeHTML($html, true, false, false, false, '');
$pdf->SetFont('times', 'B', 10);
$pdf->SetXY(15,103);
$pdf->Cell( 0, 0, 'SCANNER', 0, false, 'L', 0, '', 0, false, 'M', 'M');
$pdf->ln(10);
$pdf->SetX(20);
$pdf->Cell(30, 0, 'Código Examen', 1, $ln=0, 'C', 7, '', 0, false, '', '');
$pdf->Cell(90, 0, 'Nombre Examen', 1, $ln=0, 'C', 0, '', 0, false, '', '');
$pdf->Cell(30, 0, 'Contraste', 1, $ln=0, 'C', 0, '', 0, false, '', '');
$pdf->SetFont('times', '', 10);
$rs = Zen::Consulta("SELECT	UPPER(EXOT.EX_CODIGO) AS CODIGO_EXAMEN, UPPER(EX.EX_NOMBRE) AS NOMBRE_EXAMEN, CASE WHEN EXOT.EXOT_CONTRASTE = '1' THEN 'SI' ELSE 'NO' END AS CONTRASTE
FROM	EXAMENES_OT AS EXOT JOIN EXAMENES AS EX ON EXOT.EX_CODIGO = EX.EX_CODIGO
WHERE 	OT_ID = $id;");
foreach ($rs as $k => $record){ 
$pdf->ln(4.5);
$pdf->SetX(20);
$pdf->Cell(30, 0, $record['codigo_examen'], 1, $ln=0, 'L', 7, '', 0, false, '', '');
$pdf->Cell(90, 0, $record['nombre_examen'], 1, $ln=0, 'L', 0, '', 0, false, '', '');
$pdf->Cell(30, 0, $record['contraste'], 1, $ln=0, 'L', 0, '', 0, false, '', '');}
ob_end_clean();
$pdf->Output('r21.pdf', 'D');
  }
  else
  {

     Zen::LogoutModulo();

  }

?> 